package com.frxuexi.park2209.notice

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.base.entity.NoticeBean
import com.frxuexi.base.entity.NoticeBeanItem
import com.frxuexi.base.mvvm.BaseActivity
import com.frxuexi.park2209.R
import com.frxuexi.park2209.adapter.NoticeAdapter
import com.frxuexi.park2209.adapter.NoticeAdapter2
import com.frxuexi.park2209.databinding.ActivityNoticeBinding
import com.google.gson.Gson
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/NoticeActivity")
@AndroidEntryPoint
class NoticeActivity : BaseActivity<ActivityNoticeBinding,NoticeViewModel>() {
    lateinit var noticeAdapter: NoticeAdapter
    lateinit var noticeAdapter2: NoticeAdapter2
    var page=0
    override fun initData() {
        lifecycleScope.launch {
            vm.noticeSuc.observe(this@NoticeActivity,object :Observer<String>{
                override fun onChanged(value: String) {
                    if(value.isNotEmpty()){
                        val noticeBean = Gson().fromJson(value, NoticeBean::class.java)
                        val list = mutableListOf<NoticeBeanItem>()
                        for (i in 0..noticeBean.size-1){
                            val item = noticeBean.get(i)
                            item.type=i%2
                            list.add(item)
                        }
//                    noticeAdapter.addData(list)
//                    noticeAdapter.notifyDataSetChanged()
                        noticeAdapter2.addData(list)
                        noticeAdapter2.notifyDataSetChanged()
                    }
                }

            })
            /*vm.noticeSuc.collect{
                if(it.isNotEmpty()){
                    val noticeBean = Gson().fromJson(it, NoticeBean::class.java)
                    val list = mutableListOf<NoticeBeanItem>()
                    for (i in 0..noticeBean.size-1){
                        val item = noticeBean.get(i)
                        item.type=i%2
                        list.add(item)
                    }
//                    noticeAdapter.addData(list)
//                    noticeAdapter.notifyDataSetChanged()
                    noticeAdapter2.addData(list)
                    noticeAdapter2.notifyDataSetChanged()
                }
            }*/
        }
    }

    override fun initView() {
        noticeAdapter= NoticeAdapter()
        noticeAdapter2= NoticeAdapter2()
        binding.rvNotice.apply {
//            adapter=noticeAdapter
            adapter=noticeAdapter2
            layoutManager=LinearLayoutManager(this@NoticeActivity)
        }

        binding.headerNotice.headerTitle.text="公告管理"
        binding.headerNotice.ivBack.setOnClickListener {
            finish()
        }

        binding.smart.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                noticeAdapter2.data.clear()
                page=0
                vm.getNotices(mapOf("page" to page))
                binding.smart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                noticeAdapter2.data.clear()
                page++
                vm.getNotices(mapOf("page" to page))
                binding.smart.finishLoadMore()
            }

        })

        binding.headerNotice.headerAdd.apply {
            visibility=View.VISIBLE
            setOnClickListener {
                startActivity(Intent(this@NoticeActivity,AddNoticeActivity::class.java))
            }
        }

        // 左滑删除
        val touchHelper =
            ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
                override fun onMove(
                    recyclerView: RecyclerView,
                    viewHolder: RecyclerView.ViewHolder,
                    target: RecyclerView.ViewHolder
                ): Boolean {
                    return false
                }

                override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                    val position = viewHolder.adapterPosition
                    noticeAdapter2.notifyItemRemoved(position)
                }

            })

        touchHelper.attachToRecyclerView(binding.rvNotice)

    }

    override fun onResume() {
        super.onResume()
        noticeAdapter2.data.clear()
        vm.getNotices(mapOf("page" to page))
    }

}